const express = require("express")
const blog = require("../service/blogService")
const { formatResponse } = require("../utils/tool")
    // const { UnknownError } = require("../utils/error")

// 分类
const router = express.Router()

// 添加
router.post("/", async function(req, res, next) {
    const result = await blog.addBlog(req.body)
    res.send(result)

})

// 分页获取博客
router.get("/", async function(req, res, next) {
    res.send(await blog.findblogByPage(req.query));
})

// 获取其中一个博客
router.get("/:id", async function(req, res, next) {
    const reqHeaders = req.headers.authentication; // 获取请求头中的token是否存在
    res.send(await blog.findBlogById(req.params.id, reqHeaders));
})

// 修改其中一个博客
router.put("/:id", async function(req, res, next) {
    res.send(await blog.updateBlog(req.params.id, req.body));
})

// 删除其中一个博客
router.delete("/:id", async function(req, res, next) {
    res.send(await blog.deleteBlog(req.params.id));
})




module.exports = router